/*
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package com.germinus.telcoblocks.echarts.Click2Dial;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Iterator;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.echarts.servlet.sip.JavaToEChartsMachine;

/**
 * 
 * @author snavarro
 * 
 *         Based on Mobicent SimpleWebServlet adapted to E4SS
 */

public class ClickWebServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static Logger logger = Logger.getLogger(ClickWebServlet.class);

	@Override
	public void init(ServletConfig config) throws ServletException {
		super.init(config);
		logger.info("ClickWebServlet initialized");
	}

	/**
	 * Handle the HTTP GET method by building a simple web page.
	 */
	@SuppressWarnings("deprecation")
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String toAddr = request.getParameter("to");
		String fromAddr = request.getParameter("from");
		String bye = request.getParameter("bye");
		logger.info("Running ECharts");

		CallStatusContainer calls = CallStatusContainer
				.getCallStatusContainer();

		if (bye != null) {
			if (bye.equals("all")) {
				HashSet<Call> callsAux = calls.getCalls();
				Iterator<Call> it = callsAux.iterator();
				while (it.hasNext()) {
					Call call = it.next();
					String sasId = call.getSession();
					try {
						((JavaToClick2DialEChartsMachine) JavaToEChartsMachine
								.getInstance(sasId)).dropCalls();
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				}
				calls.removeAllCall();
			} else {
				// Someone wants to end an established call, send byes and clean
				// up
				Call call = calls.getCall(fromAddr, toAddr);
				String sasId = call.getSession();
				try {
					((JavaToClick2DialEChartsMachine) JavaToEChartsMachine
							.getInstance(sasId)).dropCalls();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		} else {

			String sasid;

			try {
				JavaToClick2DialEChartsMachine j2c2d = JavaToClick2DialEChartsMachine
						.initiate(fromAddr, toAddr, -1, true);
				sasid = j2c2d.getApplicationSessionId();
				logger.info("Running Echart Machine with id = " + sasid);

			} catch (Exception e) {
				throw new ServletException(e);
			}

		}

		// Write the output html
		PrintWriter out;
		response.setContentType("text/html");
		out = response.getWriter();

		// Just redirect to the index
		out
				.println("<HTML><META HTTP-EQUIV=\"Refresh\"CONTENT=\"0; URL=index.jsp\"><HEAD><TITLE></HTML>");
		out.close();
	}

}
